/*
    倒记时插件
    @requires jQuery v1.2 or above
    @example
    var countdown = new Countdown("2011-06-19 12:11:12", function(date){
        console.log(date.d + "天" + date.h + "时" + date.m "分"+ date.s +"秒")
    }, function(){
        console.log("finsih.")
    });
*/

define("countdown/countdown-debug",['jquery'],function (require, exports, module) {

    'use strict';

    var $ = require("jquery");

    var fetchServerTime = function (callback) {
        $.ajax({
            type: "GET",
            dataType: "jsonp",
            jsonp: "jsoncallback",
            url: "http://www.mbaobao.com/ajax/getTime?d=" + Math.floor((Math.random() * 100) + 1),
            success: function (json) {
             
                var jsTime = json.data.replace(/-/g, "/");
           
                callback(isNaN(Date.parse(jsTime)) ? (new Date()).getTime() : (new Date(jsTime)).getTime());
                  
            },
            error: function () {
                callback((new Date()).getTime());
            }
        })
    };

    var Interval = function () {
        var self = this;
        self.funArr = [];
        self.time;

        fetchServerTime(function (serverTime) {
         
            var nowTime = serverTime;
            self.time = setInterval(function () {
                nowTime = nowTime + 1000;

                for (var i = 0; i < self.funArr.length; i++) {

                    self.funArr[i] && self.funArr[i](nowTime);
                }
            }, 1000);
        });

    }



    Interval.prototype.push = function (processWrap) {

        return this.funArr.push(processWrap);
    }
    var interval;

    var Countdown = function (endtime, process, finish) {

        if (!endtime) return false;

        var _endtime = endtime.replace(/-/g, "/");

        if (isNaN(Date.parse(_endtime))) return false;
console.log(_endtime)
        var end_s = (new Date(_endtime)).getTime();
console.log(end_s)
        if (!interval) {
            interval = new Interval();
        }

        var intervalId = interval.push(function (now_s) {

            var d, h, m, s;
            var leftsecond = parseInt((end_s - now_s) / 1000);

            d = parseInt(leftsecond / 3600 / 24);
            console.log(d)
            h = parseInt((leftsecond / 3600) % 24);
            m = parseInt((leftsecond / 60) % 60);
            s = parseInt(leftsecond % 60);

            if (s < 0) {
                // interval.remove(intervalId);
                d = h = m = s = 0;
                finish && finish();
            } else {
                process && process({
                    d: d,
                    h: h,
                    m: m,
                    s: s,
                    dd: (d < 10 ? "0" : "") + d,
                    hh: (h < 10 ? "0" : "") + h,
                    mm: (m < 10 ? "0" : "") + m,
                    ss: (s < 10 ? "0" : "") + s
                });
            }
        });
   

    };

    Countdown.stopAll = function () {
        interval && interval.stopAll();
    }

    return Countdown;

});